package com.shi.study;

import org.hyperledger.fabric.sdk.User;
import org.hyperledger.fabric.sdk.HFClient;
import org.hyperledger.fabric.sdk.Channel;
import org.hyperledger.fabric.sdk.Peer;
import org.hyperledger.fabric.sdk.Orderer;
import org.hyperledger.fabric.sdk.security.CryptoSuite;
import org.hyperledger.fabric.sdk.ChaincodeID;
import org.hyperledger.fabric.sdk.QueryByChaincodeRequest;
import org.hyperledger.fabric.sdk.ProposalResponse;
import org.hyperledger.fabric.sdk.TransactionProposalRequest;
import org.hyperledger.fabric.sdk.BlockEvent.TransactionEvent;

import javax.net.ssl.SSLSession;
import java.util.Collection;
import java.util.Properties;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

public class App{
    public static void main(String[] args) throws Exception{

/*        System.setProperty("javax.net.ssl.trustStore", pathToTrustStore);
        System.setProperty("javax.net.ssl.trustStorePassword", pw)*/

        System.out.println("counter app");

        //创建User实例
        String keyFile = "F:\\crypto-config\\peerOrganizations\\org1.example.com\\users\\Admin@org1.example.com\\" +
                "msp\\keystore\\16ddb71621fc170051061ebb1f4953cd5c97fd630814010358e336750d06afd2_sk";
        String certFile = "F:\\crypto-config\\peerOrganizations\\org1.example.com\\users\\Admin@org1.example.com\\" +
                "msp\\signcerts\\Admin@org1.example.com-cert.pem";
        LocalUser user = new LocalUser("admin","Org1MSP",keyFile,certFile);

        //创建HFClient实例
        HFClient client = HFClient.createNewInstance();
        client.setCryptoSuite(CryptoSuite.Factory.getCryptoSuite());
        client.setUserContext(user);


        //创建通道实例
        Channel channel = client.newChannel("mychannel");

        //peer相关配置
        Properties peerProperties = new Properties();
        peerProperties.setProperty("pemFile", "F:\\crypto-config\\peerOrganizations\\org1.example.com\\peers\\peer0.org1.example.com\\tls\\server.crt");
        peerProperties.setProperty("clientCertFile", "F:\\crypto-config\\peerOrganizations\\org1.example.com\\users\\Admin@org1.example.com\\tls\\client.crt");
        peerProperties.setProperty("clientKeyFile", "F:\\crypto-config\\peerOrganizations\\org1.example.com\\users\\Admin@org1.example.com\\tls\\client.key");
        peerProperties.setProperty("hostnameOverride", "peer0.org1.example.com");
        peerProperties.setProperty("sslProvider", "openSSL");
        peerProperties.setProperty("negotiationType", "TLS");
        peerProperties.setProperty("trustServerCertificate", "true");
        peerProperties.put("grpc.NettyChannelBuilderOption.maxInboundMessageSize", 9000000);
        peerProperties.put("grpc.max_send_message_length", 104857600);
        peerProperties.put("grpc.max_receive_message_length", 104857600);
        Peer peer = client.newPeer("peer0.org1.example.com","grpcs://peer0.org1.example.com:7051",peerProperties);

//        Properties peerProperties1 = new Properties();
//        peerProperties1.setProperty("pemFile", "F:\\crypto-config\\peerOrganizations\\org1.example.com\\peers\\peer1.org1.example.com\\tls\\server.crt");
//        peerProperties1.setProperty("clientCertFile", "F:\\crypto-config\\peerOrganizations\\org1.example.com\\users\\Admin@org1.example.com\\tls\\client.crt");
//        peerProperties1.setProperty("clientKeyFile", "F:\\crypto-config\\peerOrganizations\\org1.example.com\\users\\Admin@org1.example.com\\tls\\client.key");
//        peerProperties1.setProperty("hostnameOverride", "peer1.org1.example.com");
//        peerProperties1.setProperty("sslProvider", "openSSL");
//        peerProperties1.setProperty("negotiationType", "TLS");
//        peerProperties1.setProperty("trustServerCertificate", "true");
//        peerProperties1.put("grpc.NettyChannelBuilderOption.maxInboundMessageSize", 9000000);
//        peerProperties1.put("grpc.max_send_message_length", 104857600);
//        peerProperties1.put("grpc.max_receive_message_length", 104857600);
//        Peer peer1 = client.newPeer("peer1.org1.example.com","grpcs://peer1.org1.example.com:8051",peerProperties1);


        Properties peerProperties2 = new Properties();
        peerProperties2.setProperty("pemFile", "F:\\crypto-config\\peerOrganizations\\org2.example.com\\peers\\peer0.org2.example.com\\tls\\server.crt");
        peerProperties2.setProperty("clientCertFile", "F:\\crypto-config\\peerOrganizations\\org2.example.com\\users\\Admin@org2.example.com\\tls\\client.crt");
        peerProperties2.setProperty("clientKeyFile", "F:\\crypto-config\\peerOrganizations\\org2.example.com\\users\\Admin@org2.example.com\\tls\\client.key");
        peerProperties2.setProperty("hostnameOverride", "peer0.org2.example.com");
        peerProperties2.setProperty("sslProvider", "openSSL");
        peerProperties2.setProperty("negotiationType", "TLS");
        peerProperties2.setProperty("trustServerCertificate", "true");
        peerProperties2.put("grpc.NettyChannelBuilderOption.maxInboundMessageSize", 9000000);
        peerProperties2.put("grpc.max_send_message_length", 104857600);
        peerProperties2.put("grpc.max_receive_message_length", 104857600);
        Peer peer2 = client.newPeer("peer0.org2.example.com","grpcs://peer0.org2.example.com:9051",peerProperties2);

//        Properties peerProperties3 = new Properties();
//        peerProperties3.setProperty("pemFile", "F:\\crypto-config\\peerOrganizations\\org2.example.com\\peers\\peer1.org2.example.com\\tls\\server.crt");
//        peerProperties3.setProperty("clientCertFile", "F:\\crypto-config\\peerOrganizations\\org2.example.com\\users\\Admin@org2.example.com\\tls\\client.crt");
//        peerProperties3.setProperty("clientKeyFile", "F:\\crypto-config\\peerOrganizations\\org2.example.com\\users\\Admin@org2.example.com\\tls\\client.key");
//        peerProperties3.setProperty("hostnameOverride", "peer1.org2.example.com");
//        peerProperties3.setProperty("sslProvider", "openSSL");
//        peerProperties3.setProperty("negotiationType", "TLS");
//        peerProperties3.setProperty("trustServerCertificate", "true");
//        peerProperties3.put("grpc.NettyChannelBuilderOption.maxInboundMessageSize", 9000000);
//        peerProperties3.put("grpc.max_send_message_length", 104857600);
//        peerProperties3.put("grpc.max_receive_message_length", 104857600);
//        Peer peer3 = client.newPeer("peer1.org2.example.com","grpcs://peer1.org2.example.com:10051",peerProperties3);


        channel.addPeer(peer);
//        channel.addPeer(peer1);
        channel.addPeer(peer2);
//        channel.addPeer(peer3);

        //orderer相关配置
        Properties ordererProperties = new Properties();
        ordererProperties.setProperty("pemFile", "F:\\crypto-config\\ordererOrganizations\\example.com\\orderers\\orderer.example.com\\tls\\server.crt");
        ordererProperties.setProperty("clientCertFile", "F:\\crypto-config\\ordererOrganizations\\example.com\\users\\Admin@example.com\\tls\\client.crt");
        ordererProperties.setProperty("clientKeyFile", "F:\\crypto-config\\ordererOrganizations\\example.com\\users\\Admin@example.com\\tls\\client.key");
        ordererProperties.setProperty("hostnameOverride", "orderer.example.com");
        ordererProperties.setProperty("sslProvider", "openSSL");
        ordererProperties.setProperty("negotiationType", "TLS");
        ordererProperties.setProperty("trustServerCertificate", "true");
        ordererProperties.put("grpc.NettyChannelBuilderOption.keepAliveTime", new Object[]{5L, TimeUnit.MINUTES});
        ordererProperties.put("grpc.NettyChannelBuilderOption.keepAliveTimeout", new Object[]{8L, TimeUnit.SECONDS});
        ordererProperties.put("grpc.NettyChannelBuilderOption.keepAliveWithoutCalls", new Object[]{true});
        Orderer orderer = client.newOrderer("orderer.example.com","grpcs://orderer.example.com:7050",ordererProperties);



        channel.addOrderer(orderer);
        channel.initialize();

        //查询链码
        QueryByChaincodeRequest req = client.newQueryProposalRequest();
        ChaincodeID cid = ChaincodeID.newBuilder().setName("mycc").build();
        req.setChaincodeID(cid);
        req.setFcn("query");
        req.setArgs("a");
        ProposalResponse[] rsp = channel.queryByChaincode(req).toArray(new ProposalResponse[0]);
        System.out.format("rsp message => %s\n",rsp[0].getProposalResponse().getResponse().getPayload().toStringUtf8());

        //提交链码交易
        TransactionProposalRequest req2 = client.newTransactionProposalRequest();
        req2.setChaincodeID(cid);
        req2.setFcn("invoke");
        req2.setArgs("a","b","10");
        Collection<ProposalResponse> rsp2 = channel.sendTransactionProposal(req2,channel.getPeers());
        TransactionEvent event = channel.sendTransaction(rsp2,client.getUserContext()).get();
        System.out.format("txid: %s\n", event.getTransactionID());
        System.out.format("valid: %b\n", event.isValid());
    }
}
